package com.sinoframework.web.servlet.interceptor.mybatis;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.jeecg.config.mybatis.handler.UploadFileInfoHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;


@Component
@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) })
public class MybatisConfirFiledInterceptor implements Interceptor {

	@Autowired
	private UploadFileInfoHandler uploadFileInfoHandler;

	@Override
	public Object intercept(Invocation invocation) throws Throwable {

		MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
		SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
		Object parameter = invocation.getArgs()[1];
		if (SqlCommandType.INSERT == sqlCommandType) {
			return uploadFileInfoHandler.insert(invocation,parameter,mappedStatement);
		}else if (SqlCommandType.UPDATE == sqlCommandType) {
			return uploadFileInfoHandler.update(invocation,parameter,mappedStatement);
		}else if (SqlCommandType.DELETE == sqlCommandType) {
			return uploadFileInfoHandler.delete(invocation,parameter,mappedStatement);
		}
		return invocation.proceed();
	}

	@Override
	public Object plugin(Object target) {
		return Plugin.wrap(target, this);
	}

	@Override
	public void setProperties(Properties properties) {
//		map.put("oss_file_meta",Arrays.asList("url"));
	}


}
